import wx
import gobject
import xmmsglib
import playlist
from data import *


class AngelGUI(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.CAPTION|wx.CLOSE_BOX
        wx.Frame.__init__(self, *args, **kwds)
        
        self.status = 0
        self.__set_properties()
        self.__do_layout()
        
    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle("Angel Player")
        _icon = wx.EmptyIcon()
        _icon.CopyFromBitmap(wx.Bitmap(player_images["play"], wx.BITMAP_TYPE_ANY))
        self.SetIcon(_icon)
        
        
        self.wildcard = "Python source (*.py)|*.py|"     \
                       "Compiled Python (*.pyc)|*.pyc|" \
                       "SPAM files (*.spam)|*.spam|"    \
                       "Egg file (*.egg)|*.egg|"        \
                       "All files (*.*)|*.*"

    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        self.panel_xmms = wx.BoxSizer(wx.HORIZONTAL)
        self.panel_playlist = playlist.playlist(self)

        self.panel_xmms.Add(self.panel_playlist, 1, 0, 0)
        
        self.SetSizer(self.panel_xmms)
        self.panel_xmms.Fit(self)
        self.Layout()
        # end wxGlade
        
    def get_playlist_list(self):
        return self.panel_playlist.list

    def setAngel(self, Angel):
        """
            Asigna la conexion Angel
        """
        if self.getAngel() == None:
            self.Angel = Angel
            self.setAsynch()
        else:
            print "Angel ya fue asignado"
    
    def getAngel(self):
        """
            Retorna la conexion angel
        """
        try:
            return self.Angel
        except:
            return None
    
    def setAsynch(self):
        """
            hacemos Angel asincrono
        """
        self.ml = gobject.MainLoop(None, False)
        conn = xmmsglib.GLibConnector(self.Angel)

        
       
        self.Angel.broadcast_playlist_changed(self.panel_playlist.playlist_changed)
        self.Angel.broadcast_playback_current_id(self.panel_playlist.set_track)
        
        self.panel_playlist.playlist_generate()
        self.Angel.playback_status(self.panel_playlist.setStatus)
        self.Angel.playback_current_id(self.panel_playlist.set_track)
        
        #playtime
        gobject.timeout_add(500,self.panel_playlist.setPlayTimeTrack)
        
    def myerror(self, e):
        print "Ocurrio un error", e
        sys.exit()
        
    def __del__(self):
        self.ml.quit()
    
    def Add_files(self):
        dlg = wx.FileDialog(
            self, message="Agregar Archivos Angel",
            defaultDir=os.getcwd(), 
            defaultFile="",
            wildcard=self.wildcard,
            style=wx.OPEN 
        )
        dlg.ShowModal()